<template>
  <el-container>
      <el-header>
        <h3>教练列表</h3>
      </el-header>
      <el-main>
        <el-form :inline="true" :model="formData" ref="form">
          <el-form-item prop="school_id" label="选择分校:">
            <el-select v-model="formData.school_id">
              <el-option
                v-for="item in coachList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="query_field">
            <el-input type="text" v-model.trim="formData.query_field" placeholder="输入教练手机号或者姓名" @keypress.enter.native="query" suffix-icon="el-icon-search"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  icon="el-icon-search" @click.prevent="query">查询</el-button>
            <el-button type="primary"  icon="el-icon-refresh" @click.prevent="reset('form')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="tableData"
          border
          v-loading="loading"
        >
          <el-table-column
            label="图像"
          >
            <template slot-scope="scope">
              <el-avatar :src="scope.row.avatar" :size="50"></el-avatar>
            </template>
          </el-table-column>
          <el-table-column
            prop="name"
            label="姓名"
          ></el-table-column>
          <el-table-column
            prop="school_name"
            label="所属驾校"
          ></el-table-column>
          <el-table-column
            prop="mobile"
            label="手机号码"
          ></el-table-column>
          <el-table-column
            prop="age"
            label="年龄"
          ></el-table-column>
          <el-table-column
            prop="teach_age"
            label="教龄"
          ></el-table-column>
          <el-table-column
            label="属下学员"
          >
            <template slot-scope="scope">
              <el-link v-if="scope.row.student_total" @click="jumpStudent(scope.row.id)">{{scope.row.student_total}}</el-link>
              <el-link v-else>{{scope.row.student_total}}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="招生人数"
          >
            <template slot-scope="scope">
              <el-link v-if="scope.row.enrollment_total" @click="jumpEnrollment(scope.row.id)">{{scope.row.enrollment_total}}</el-link>
              <el-link v-else>{{scope.row.enrollment_total}}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="满意率"
          >
            <template slot-scope="scope">
              <el-link @click="jumpEvaluate(scope.row.id)"> {{scope.row.good_rating}}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="及格率"
          >
            <template slot-scope="scope">
              <el-link>{{scope.row.pass_rating}}</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="约车权限"
          >
            <template slot-scope="scope">
              <el-switch v-model="scope.row.auth" disabled></el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="教学记录"
          >
            <template slot-scope="scope">
              <el-link @click="jumpTeach(scope.row.id)">查看</el-link>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
          >
            <template slot-scope="scope">
              <el-button type="primary" size="small" icon="el-icon-view" @click.prevent="info(scope.row.id)">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-footer>
        <el-pagination
          background
          layout="prev, pager, next, total"
          :total="total"
          :current-page="page"
          :page-size="pageSize"
          @current-change="changePage"
        ></el-pagination>
      </el-footer>
  </el-container>
</template>

<script>
  import {getAllSchoolList, searchCoachList} from '@/api/coach'
    export default {
        name: "coachList",
        data () {
            return {
                coachList: [
                    {
                        id: 0,
                        name: '全部'
                    },
                ],
                formData: {
                  query_field: '',
                  school_id: 0
                },
                tableData: [],
                loading: true,
                total: 0,
                page: 1,
                pageSize: 10,
            };
        },
        methods: {
          query () {
              var params = {
                  school_id: this.formData.school_id,
              };
              if (this.formData.query_field.length != 0) {
                  params.query_field = this.formData.query_field;
              }
              this.loading = true;
              searchCoachList(params).then(response => {
                  this.tableData = response.data.list;
                  this.page = Number(response.data.paginate.current);
                  this.pageSize = Number(response.data.paginate.pageSize);
                  this.total = Number(response.data.paginate.total);
                  this.loading = false;
              }).catch( err => {
                  this.loading = false;
              })
          },
          reset (ref) {
            this.$refs[ref].resetFields();
          },
          jumpStudent (coach_id)
          {
              this.$router.push({path: '/coach/studentList', query: {coach_id: coach_id}});
          },
          jumpEnrollment (coach_id)
          {
              this.$router.push({path: '/coach/enrollmentList',query: {coach_id: coach_id}});
          },
          jumpEvaluate (coach_id)
          {
              this.$router.push({path: '/coach/evaluateList', query:{coach_id: coach_id}});
          },
          jumpTeach (coach_id)
          {
              this.$router.push({path: '/coach/teach', query: {coach_id: coach_id}});
          },
          info (coach_id)
          {
              this.$router.push({path: '/coach/info', query: {coach_id: coach_id}});
          },
          changePage (currentPage)
          {
              var params = {
                  page: currentPage,
                  pageSize: this.pageSize,
                  school_id: this.formData.school_id
              };
              if (this.formData.query_field.length != 0) {
                  params.query_field = this.form.query_field;
              }
              this.loading = true;
              searchCoachList(params).then(response => {
                  this.tableData = response.data.list;
                  this.page = Number(response.data.paginate.current);
                  this.pageSize = Number(response.data.paginate.pageSize);
                  this.total = Number(response.data.paginate.total);
                  this.loading = false;
              }).catch( err => {
                  this.loading = false;
              })
          }
        },
        mounted() {
            getAllSchoolList().then(response => {
              this.coachList = this.coachList.concat(response.data);
              //获取默认的所有的教练列表信息
              searchCoachList().then(response => {
                  this.tableData = response.data.list;
                  this.loading = false;
                  this.page = response.data.paginate.current;
                  this.pageSize = response.data.paginate.pageSize;
                  this.total = response.data.paginate.total;
              })
            }).catch(err => {
            })
        }
    }
</script>

<style scoped>
  .el-container {
    width: 95%;
    max-width: 100%;
    position: relative;
    overflow: hidden;
    border: 1px solid #F2F6FC;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
    margin: 10px auto;
  }

  .el-container .el-header h3 {
    font-size: 20px;
    text-align: center;
    line-height: 20px;
    height: 20px;
  }

  .el-footer {
    text-align: center;
  }
</style>
